<template>
    <vol-box
      :lazy="true"
      v-model="model"
      title="售后待回收旧件"
      :height="400"
      :width="1200"
      :padding="5"
      :onModelClose="onModelClose"
    >
      <afterRecycleNot
        ref="afterRecycleNot"
      ></afterRecycleNot>
      <template #footer>
        <div>
          <el-button type="primary" size="small" @click="confirmBtn"
            >确认</el-button
          >
        </div>
      </template>
    </vol-box>
  </template>
      
      <script>
  import VolBox from "@/components/basic/VolBox.vue";
  import afterRecycleNot from "@/views/aftersaleinfo/q_after_recycle_not/q_after_recycle_not.vue";
  import { ElMessage } from "element-plus";
  
  export default {
    components: {
      "vol-box": VolBox,
      afterRecycleNot,
    },
    data() {
      return {
        model: false,
      };
    },
    methods: {
      async onModelClose() {
        // this.$refs.afterRecycleNot.searchFormFields.采购申请单号 = null;
      },
      async open(WHSE_no) {
        let setIntervalTiem = setInterval(() => {
          if (this.$refs.afterRecycleNot) {
            clearInterval(setIntervalTiem);
            this.$refs.afterRecycleNot.searchFormFields.WHSE_no = WHSE_no;
            this.$refs.afterRecycleNot.searchFormOptions.forEach(item => {
                item.forEach(x => {
                  if(x.field == "WHSE_no"){
                    x.disabled = true
                  }
                })
             })
            this.$refs.afterRecycleNot.$refs.grid.search();
          }
        }, 100);
        this.model = true;
      },
      async confirmBtn() {
        let rowObj =
          this.$refs.afterRecycleNot.$refs.grid.getSelectRows();
        if (!rowObj.length) return this.base.ElMessageFn("请至少选择一个商品");
        this.$emit("parentCall", ($parent) => {
          try {
            let parentList = $parent.$refs.detail.rowData;
            let set = new Set(parentList.map((item) => item.After_deliveryList)); //得到由明细表goods_sub组成的数组
            let commonObjects = rowObj.filter((item) => set.has(item.After_deliveryList)); //得到明细表中已包含的选中行
            let goodsNamesSet = new Set(
              commonObjects.map((item) => item.After_deliveryList)
            ); //相同商品的编码
            console.log("commonObjects",commonObjects);
            if (commonObjects.length) {
                rowObj = rowObj.filter(
                (item) => !goodsNamesSet.has(item.After_deliveryList)
              ); //在选中数据中剔除明细表已包含的行数据
              let str = "";
              commonObjects.forEach((r) => {
                if (str == "") str += r.Goods_name;
                else str = str + "," + r.Goods_name;
              });
              this.$message.warning(`商品${str}已存在此明细单`);
            }
            
            let arr = rowObj.map((item) => {
              return {
                After_deliveryList: item.After_deliveryList,
                Voucher_sub: item.Voucher_sub,
                Client_name: item.Client_name,
                // Goods_price:item.ID,
                Fittings_delivery_date: item.AuditDate,
                Goods_sub: item.Goods_sub,
                Goods_name: item.Goods_name,
                Goods_size: item.Goods_size,
                AvailableQuantity: item.Goods_num,
                Goods_num: item.Goods_num,
              };
            });
            $parent.$refs.detail.rowData.unshift(...arr);
          } catch (error) {
            console.error(error);
          }
        });
        this.onModelClose();
        return (this.model = false);
      },
    },
  };
  </script>
      
      <style>
  </style>